﻿using System;
using System.Collections.Generic;
using System.Text;

namespace System
{
    public static class StringExtensions
    {
        public static bool IsNullOrEmpty(this string str)
        {
            return string.IsNullOrEmpty(str);
        }
        public static bool IsNotNullOrEmpty(this string str)
        {
            return !string.IsNullOrEmpty(str);
        }

        public static bool IsNullOrWhiteSpace(this string str)
        {
            return !string.IsNullOrWhiteSpace(str);
        }
        public static bool IsNotNullOrWhiteSpace(this string str)
        {
            return !string.IsNullOrWhiteSpace(str);
        }
        public static bool EqualsIgnoreCase(this string source, string str)
        {
            return string.Compare(source, str, true) == 0;
        }
        /// <summary>
        /// 截取字符串的指定位置指定长度的子字符串。如果指定的位置或长度超出源字符串范围此方法并不会报错，而是返回范围内的子字符串。
        /// </summary>
        /// <param name="str"></param>
        /// <param name="start"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string SubstringUnsafe(this string str, int start, int length)
        {
            if (str == null)
            {
                return null;
            }
            if (str.IsNullOrEmpty())
            {
                return string.Empty;
            }
            if (str.Length <= start)
            {//起始索引超出范围
                return string.Empty;
            }
            return str.Substring(start, Math.Min(length, str.Length - start));
        }
        /// <summary>
        /// 截取字符串左边
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Left(this string str, int length)
        {
            return str.SubstringUnsafe(0, length);
        }
        /// <summary>
        /// 截取字符串右边
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Right(this string str, int length)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }
            if (str.Length <= length)
            {
                return str;
            }
            return SubstringUnsafe(str, str.Length - length, length);
        }

        /// <summary>
        /// 使用空白字符分割字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string[] SplitByWhiteSpace(this string str)
        {
            if (str.IsNullOrWhiteSpace())
            {
                return new string[0];
            }
            return str.Split(StringHelper.WhiteSpaces, StringSplitOptions.RemoveEmptyEntries);
        }
        /// <summary>
        /// 检查字符串是否符合规则
        /// </summary>
        /// <param name="str"></param>
        /// <param name="rules"></param>
        public static void Check(this string str, StringRule rules, string errtemp = "您的输入包含不允许的字符。")
        {
            if (str.IsNullOrEmpty())
            {
                if ((rules & StringRule.NotEmpty) == StringRule.NotEmpty)
                {
                    throw new Exception("必须填写。");
                }
                else
                {//允许为空
                    return;
                }
            }

            foreach (var c in str)
            {
                if ((rules & StringRule.Digit) == StringRule.Digit)
                { //指定了数字
                    if (c >= '0' && c <= '9')
                    {
                        continue;
                    }
                }
                else if ((rules & StringRule.a_z) == StringRule.a_z)
                {//指定了小写字母
                    if (c >= 'a' && c <= 'z')
                    {
                        continue;
                    }
                }
                else if ((rules & StringRule.A_Z) == StringRule.A_Z)
                {//指定了大写字母
                    if (c >= 'A' && c <= 'Z')
                    {
                        continue;
                    }
                }
                else if ((rules & StringRule.Chinese) == StringRule.Chinese)
                {//指定了汉字
                    var v = (int)c;
                    if (c >= 0x4e00 && c <= 0x9fbb)
                    {
                        continue;
                    }
                }
                else if ((rules & StringRule.Dot) == StringRule.Dot)
                {//小数点
                    if (c == '.')
                    {
                        continue;
                    }
                }
                else if ((rules & StringRule.Underline) == StringRule.Underline)
                {//下划线 
                    if (c == '_')
                    {
                        continue;
                    }
                }
                else if ((rules & StringRule.Space) == StringRule.Space)
                {//空格 
                    if (c == ' ')
                    {
                        continue;
                    }
                }
                else if ((rules & StringRule.Comma) == StringRule.Comma)
                {//逗号 
                    if (c == ',')
                    {
                        continue;
                    }
                }
                else if ((rules & StringRule.CommonSymbol) == StringRule.CommonSymbol)
                {//常见符号
                    var cs = "~!@#$%^&*()-+=[]/?|.,{}";
                    if (cs.Contains(c.ToString()))
                    {
                        continue;
                    }
                }

                throw new Exception(errtemp);

            }

        }
    }
    /// <summary>
    /// 字符串规则
    /// </summary>
    [Flags]
    public enum StringRule
    {
        /// <summary>
        /// 不允许为空
        /// </summary>
        NotEmpty = 1,
        /// <summary>
        /// 纯数字
        /// </summary>
        Digit = 2,
        /// <summary>
        /// 小写字母
        /// </summary>
        a_z = 4,
        /// <summary>
        /// 大写字母
        /// </summary>
        A_Z = 8,
        /// <summary>
        /// 汉字
        /// </summary>
        Chinese = 16,
        /// <summary>
        /// 小数点
        /// </summary>
        Dot = 32,
        /// <summary>
        /// 下划线
        /// </summary>
        Underline = 64,
        /// <summary>
        /// 常见符号（@ & # ~ * / ! + - =）
        /// </summary>
        CommonSymbol = 128,
        /// <summary>
        /// 半角空格
        /// </summary>
        Space = 256,
        /// <summary>
        /// 半角逗号
        /// </summary>
        Comma = 512
    }
}
